package com.java.multipledatasource.algorithm;

/**
 * @author hu
 * @version 1.0
 * @date 2021/1/19 0019 下午 1:36
 * @description 数组实现队列
 */
public class ArrayQueue {

    static class MyQueue {
        private int[] arr;
        private int pushI;
        private int pollI;
        private int size;
        private int limit;

        MyQueue(int limit) {
            arr = new int[limit];
            pushI = 0;
            pollI = 0;
            size = 0;
            limit = limit;
        }

        public void push(int value) {
            if (size == limit) {
                throw new RuntimeException("栈满了");
            }
            size++;
            arr[pollI] = value;
            pushI = nextIndex(pushI);
        }

        public int pop () {
            if (size == 0) {
                throw  new RuntimeException("栈为空");
            }
            size--;
            int res = arr[pollI];
            pollI = nextIndex(pollI);
            return  res;
        }

        public  int nextIndex(int currentIndex) {
            return  currentIndex < limit -1 ? currentIndex + 1 : 0;
        }
    }
}
